package com.project.class3_29;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.List;

public class XMLPress {

    public List<StudentBean> press(){
        List<StudentBean> studentList = new ArrayList<>();

        SAXReader sax = new SAXReader();
        try {
            //读取文档内容，加载至Document文档对象中
            Document doc = sax.read("xml/lovoClass.xml");
            //选择lovoClass/student标记进行解析。每个student标记就是一个Element元素
            List<Element> elementList = doc.selectNodes("/lovoClass/student");

            for(Element element : elementList){
                StudentBean student = new StudentBean();
                //将student标记的code属性的值取出，封装student对象的code属性
                student.setCode(element.attributeValue("code"));
                student.setTel(element.attributeValue("tel"));

                //将student标记的子标记name的标记内容取出，封装student对象的name属性
                student.setName(element.elementText("name"));
                student.setEdu(element.elementText("edu"));
                student.setAge(Integer.parseInt(element.elementText("age")));

                studentList.add(student);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

        return studentList;
    }

    public static void main(String[] args) {
        XMLPress xml = new XMLPress();
        System.out.println(xml.press());
    }
}
